#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018, Raphael Lehmann
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

class Instance(Module):
    def __init__(self, instance):
        self.instance = instance

    def init(self, module):
        module.name = str(self.instance)
        module.description = "Instance {}".format(self.instance)

    def prepare(self, module, options):
        module.depends(":platform:comp")
        return True

    def build(self, env):
        device = env[":target"]
        driver = device.get_driver("comp")

        properties = device.properties
        properties["target"] = device.identifier
        instance_id = int(self.instance)
        properties["id"] = instance_id
        properties["driver"] = driver
        properties["csr"] = "COMP_CSR_" if device.identifier["family"] in ["l4", "g4", "g0"] else "COMP_CSR_COMPx"
        properties["blanking_source"] = dict()
        if device.identifier["family"] in ["g4"]:
            properties["blanking_source"]["b001"] = ["Tim1Oc5", "Tim1Oc5", "Tim1Oc5", "Tim3Oc4", "Tim2Oc3", "Tim8Oc5", "Tim1Oc5"][instance_id - 1]
            properties["blanking_source"]["b010"] = ["Tim2Oc3", "Tim2Oc3", "Tim3Oc3", "Tim8Oc5", "Tim8Oc5", "Tim2Oc4", "Tim8Oc5"][instance_id - 1]
            properties["blanking_source"]["b011"] = ["Tim3Oc3", "Tim3Oc3", "Tim2Oc4", "Tim15Oc1_2", "Tim3Oc3", "Tim15Oc2", "Tim3Oc3"][instance_id - 1]
            properties["blanking_source"]["b100"] = ["Tim8Oc5", "Tim8Oc5", "Tim8Oc5", "Tim1Oc5", "Tim1Oc5", "Tim1Oc5", "Tim15Oc2"][instance_id - 1]
            properties["blanking_source"]["b101"] = ["Tim20Oc5", "Tim20Oc5", "Tim20Oc5", "Tim20Oc5", "Tim20Oc5", "Tim20Oc5", "Tim20Oc5"][instance_id - 1]
            properties["blanking_source"]["b110"] = ["Tim15Oc1", "Tim15Oc1", "Tim15Oc1", "Tim15Oc1", "Tim15Oc1", "Tim15Oc1", "Tim15Oc1"][instance_id - 1]
            properties["blanking_source"]["b111"] = ["Tim4Oc3", "Tim4Oc3", "Tim4Oc3", "Tim4Oc3", "Tim4Oc3", "Tim4Oc3", "Tim4Oc3"][instance_id - 1]
        elif device.identifier["family"] in ["l4"]:
            properties["blanking_source"]["b001"] = ["Tim1Oc5", None][instance_id - 1]
            properties["blanking_source"]["b010"] = ["Tim2Oc3", None][instance_id - 1]
            properties["blanking_source"]["b100"] = [None, "Tim15Oc1"][instance_id - 1]
        elif device.identifier["family"] in ["f3"]:
            properties["blanking_source"]["b001"] = "Tim1Oc5"
            properties["blanking_source"]["b010"] = "Tim2Oc3"
            properties["blanking_source"]["b011"] = "Tim3Oc3"
        elif device.identifier["family"] in ["g0"]:
            properties["blanking_source"]["b00000"] = None
            properties["blanking_source"]["b00001"] = "Tim1Oc4"
            properties["blanking_source"]["b00010"] = "Tim1Oc5"
            properties["blanking_source"]["b00100"] = "Tim2Oc3"
            properties["blanking_source"]["b01000"] = "Tim3Oc3"
            properties["blanking_source"]["b10000"] = "Tim15Oc2"



        env.substitutions = properties
        env.outbasepath = "modm/src/modm/platform/comp"

        env.template("comp.hpp.in", "comp_{}.hpp".format(self.instance))


def init(module):
    module.name = ":platform:comp"
    module.description = "Comparator (COMP)"

def prepare(module, options):
    device = options[":target"]

    if not device.has_driver("comp:stm32*"):
        return False

    """
    Existing comparator IPs:
        "stm32-tsmc90_cube"
        "stm32-tsmc90_dory_cube"
        "stm32-tsmc90_g4_rockfish_cube"
        "stm32-tsmc90_h7_cube"
        "stm32-tsmc90_orca128_cube"
        "stm32-tsmc90_orca512_cube"
        "stm32-tsmc90_orcazero_cube"
        "stm32-v1.0"
        "stm32-v1.2"
        "stm32-v1.3"
        "stm32-v3.4"
        "stm32-v3.6"
    """
    if not device.get_driver("comp")["type"] in ["stm32-v1.3", "stm32-tsmc90_cube", "stm32-tsmc90_g4_rockfish_cube", "stm32-tsmc90_orcazero_cube"]:
        return False

    # Only some STM32F3 and STM32L4
    if device.identifier["family"] == "f3":
        if not device.identifier["name"] in ["03", "28", "58", "98"]:
            return False
    elif device.identifier["family"] == "l4":
        if not device.identifier["name"] in ["31", "32", "33", "42", "43", "51", "52", "62"]:
            return False
    elif device.identifier["family"] == "g4":
        pass
    elif device.identifier["family"] == "g0":
        pass
    else:
        return False

    module.depends(":cmsis:device")

    for instance in listify(device.get_driver("comp")["instance"]):
        module.add_submodule(Instance(int(instance)))

    return True

def build(env):
    device = env[":target"]
    driver = device.get_driver("comp")

    properties = device.properties
    properties["target"] = device.identifier
    properties["driver"] = driver
    properties["csr"] = "COMP_CSR_" if device.identifier["family"] in ["l4", "g4", "g0"] else "COMP_CSR_COMPx"

    env.substitutions = properties
    env.outbasepath = "modm/src/modm/platform/comp"
    env.template("base.hpp.in", "base.hpp")
